package com.mingtu.hikvideo2.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.mingtu.hikvideo2.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class HikFucAdapter extends RecyclerView.Adapter<HikFucAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private List<String> mData = new ArrayList<>();
    private String[] textArray = {"抓拍", "录像", "3D放大"};
    private OnItemClickListener mOnItemClickListener = null;
    private HashMap<Integer, Boolean> hashMap = new HashMap<>();

    public HikFucAdapter(Context context) {
        this.context = context;

        for (int i = 0; i < textArray.length; i++) {
            mData.add(textArray[i]);
            hashMap.put(i, false);
        }

    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hik_function, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {

        if (mData != null) {
            if (mData.size() > 0) {
                holder.tvText.setText(mData.get(position));
                if (position == 0) {
                    holder.checkBox.setBackground(context.getDrawable(R.drawable.hik_selector_radio_button1));
                } else if (position == 1) {
                    holder.checkBox.setBackground(context.getDrawable(R.drawable.hik_selector_radio_button2));
                } else if (position == 2) {
                    holder.checkBox.setBackground(context.getDrawable(R.drawable.hik_selector_radio_button3));
                }
                holder.checkBox.setChecked(hashMap.get(position));
            }
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void upData(List<String> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * 设置选中的状态
     *
     * @param position
     * @param b
     */
    public void setCheckEvent(int position, boolean b) {
        hashMap.put(position, b);
        notifyDataSetChanged();
    }


    /**
     * 获取选中的状态
     *
     * @param position
     * @return
     */
    public boolean getCheckEvent(int position) {
        return hashMap.get(position);
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        public LinearLayout layout;
        public CheckBox checkBox;
        public TextView tvText;


        public ActivityHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            layout = (LinearLayout) convertView.findViewById(R.id.layout);
            checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            tvText = (TextView) convertView.findViewById(R.id.tv_text);
//            ViewGroup.LayoutParams lp = ivPicture.getLayoutParams();
//            int screenWidth = ScreenUtils.getScreenWidth(); //照片一行2个，间隙是10dp
//            int mWidth = (screenWidth - SizeUtils.dp2px(10) * 3) / 2;
//            int mHeight = mWidth;
//            lp.width = mWidth;
//            lp.height = mHeight;
//            ivPicture.setLayoutParams(lp);
        }
    }

}
